
package org.jeecgframework.core.util.clean;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;

import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.DomSerializer;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.PrettyXmlSerializer;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * 
 * <p>
 * 标题: HtmlCleaner基础工具类
 * </p>
 * <p>
 * 描述: // TODO
 * </p>
 * 
 * @author LinYingPiaoJian(Q:71426052)
 * @version // TODO
 * @since Mar 28, 2014 3:09:39 PM
 */
public class HtmlCleanBaseUtil {
	/**
	 * 
	 * <p>
	 * 方法简要描述: 清楚样式
	 * </p>
	 * <p>
	 * 方法详细描述: // TODO
	 * </p>
	 * 
	 * @author LinYingPiaoJian(Q:71426052)
	 * @version 
	 * @see
	 * @param node
	 * @throws XPatherException
	 */
	public static void cleanStyle(TagNode node) throws XPatherException{
		Object[] nsscript = node.evaluateXPath("//style");
		for (Object tt : nsscript) {
			((TagNode) tt).removeFromTree();
		}
	}
	/**
	 * 
	 * <p>
	 * 方法简要描述: 清楚javascript脚本
	 * </p>
	 * <p>
	 * 方法详细描述: // TODO
	 * </p>
	 * 
	 * @author LinYingPiaoJian(Q:71426052)
	 * @version
	 * @see
	 * @return
	 * @throws XPatherException
	 */
	public static void cleanScript(TagNode node) throws XPatherException {
		Object[] nsscript = node.evaluateXPath("//script");
		for (Object tt : nsscript) {
			((TagNode) tt).removeFromTree();
		}
	}

	/**
	 * 
	 * <p>
	 * 方法简要描述: 清楚body的一些没用的标签和标识
	 * </p>
	 * <p>
	 * 方法详细描述: // TODO
	 * </p>
	 * 
	 * @author LinYingPiaoJian(Q:71426052)
	 * @version
	 * @see
	 * @param bodycontent
	 * @return
	 */
	public static String cleanTag(String bodycontent) {
		// 去掉'\n'
		bodycontent = bodycontent.replaceAll("\n", "");
		// 去掉很多'空格'
		bodycontent = bodycontent.replaceAll("( )+", " ");

		return bodycontent;
	}

//	public static List<String> getHrefList(TagNode node)
//			throws Exception {
//		return getHrefList(node, "body");
//	}

	/**
	 * 
	 * <p>
	 * 方法简要描述: 获取标签内的所有超链接
	 * </p>
	 * <p>
	 * 方法详细描述: // TODO
	 * </p>
	 * 
	 * @author LinYingPiaoJian(Q:71426052)
	 * @version
	 * @see
	 * @param node
	 * @param tag
	 * @return
	 * @throws XPatherException
	 */
//	public static List<String> getHrefList(TagNode node, String tag)
//			throws Exception {
//		List<String> hrefList = null;
//		
//		CleanerProperties props = new HtmlCleaner().getProperties();
//		Document doc = new DomSerializer(props).createDOM(node);
//		XPath xpath = XPathFactory.newInstance().newXPath();
//		String xexpression = "//"+tag+"//a[@href]";
//		System.out.println(xexpression);
//		NodeList nodes = (NodeList) xpath.evaluate(xexpression, doc,
//				javax.xml.xpath.XPathConstants.NODESET);
//		Node n = null;
//		hrefList = new ArrayList<String>();
//		for (int i = 0; i < nodes.getLength(); i++) {
//			n = nodes.item(i);
//			hrefList.add(n.getAttributes().getNamedItem("href").getNodeValue());
//		}
//		
//		
////		Object[] ns = node.evaluateXPath("//"+tag+"//a[@href]");
////		if (ns != null && ns.length > 0) {
////			hrefList = new ArrayList<String>();
////			for (Object n : ns) {
////				TagNode hrefNode = (TagNode) n;  
////				String href = hrefNode.getAttributeByName("href").trim();
////				if(StringUtil.isNotEmpty(href) && (href.startsWith("http:") || href.startsWith("https:"))){
////					if(href.endsWith("//"))	//去掉最后面的'/'
////						href = href.substring(0, href.length()-2);
////					if(!hrefList.contains(href))
////						hrefList.add(href);
////				}
////			}
////		}
//		return hrefList;
//	}
	
	public static List<String> getHrefListByXpath(TagNode node, String hrefpath) throws Exception{
		List<String> hrefList = new ArrayList<String>();
		
		CleanerProperties props = new HtmlCleaner().getProperties();
		Document doc = new DomSerializer(props).createDOM(node);
		XPath xpath = XPathFactory.newInstance().newXPath();
		String xexpression = hrefpath;
		NodeList nodes = (NodeList) xpath.evaluate(xexpression, doc,javax.xml.xpath.XPathConstants.NODESET);
		Node n = null;
		hrefList = new ArrayList<String>();
		for (int i = 0; i < nodes.getLength(); i++) {
			n = nodes.item(i);
			hrefList.add(n.getAttributes().getNamedItem("href").getNodeValue());
		}
		
//		Object[] ns = node.evaluateXPath(xpath);
//		if (ns != null && ns.length > 0) {
//			hrefList = new ArrayList<String>();
//			for (Object n : ns) {
//				TagNode hrefNode = (TagNode) n;  
//				String href = hrefNode.getAttributeByName("href").trim();
//				if(StringUtil.isNotEmpty(href) && !href.equals("#") && !href.contains("javascript")){
//					if(href.endsWith("//"))	//去掉最后面的'/'
//						href = href.substring(0, href.length()-2);
//					if(!hrefList.contains(href))
//						hrefList.add(href);
//				}
//			}
//		}
		return hrefList;
	}
	
	

	/**
	 * 
	 * <p>
	 * 方法简要描述: 获取整个页面完整的信息
	 * </p>
	 * <p>
	 * 方法详细描述: // TODO
	 * </p>
	 * 
	 * @author LinYingPiaoJian(Q:71426052)
	 * @version
	 * @see
	 * @param node
	 * @return
	 * @throws IOException
	 */
	public static String getAllContent(HtmlCleaner cleaner, TagNode node)throws IOException {
		CleanerProperties props = cleaner.getProperties();
		props.setUseCdataForScriptAndStyle(true);
		props.setRecognizeUnicodeChars(true);
		props.setUseEmptyElementTags(true);
		props.setAdvancedXmlEscape(true);
		props.setTranslateSpecialEntities(true);
		props.setBooleanAttributeValues("empty");
		return new PrettyXmlSerializer(props).getAsString(node);
	}

	/**
	 * 
	 * <p>
	 * 方法简要描述: 获取html - body的内容
	 * </p>
	 * <p>
	 * 方法详细描述: // TODO
	 * </p>
	 * 
	 * @author LinYingPiaoJian(Q:71426052)
	 * @version
	 * @see
	 * @param node
	 * @return
	 */
	public static String getBody(TagNode node) {
		Object[] ns = node.getElementsByName("body", true);
		if (ns != null && ns.length > 0)
			return "" + ((TagNode) ns[0]).getText();
		return null;
	}

	/**
	 * 
	 * <p>
	 * 方法简要描述: 获取某个标签内的正文
	 * </p>
	 * <p>
	 * 方法详细描述: // TODO
	 * </p>
	 * 
	 * @author LinYingPiaoJian(Q:71426052)
	 * @version 
	 * @see
	 * @param node
	 * @param tagXpath
	 * @return
	 * @throws Exception
	 */
	public static String getTagContent(TagNode node , String tagXpath) throws Exception{
		Object[] ns = node.evaluateXPath(tagXpath);
		String alltext = null;
		if(ns != null && ns.length>0){
			alltext = "";
			String t = null;
			for(Object n : ns){
				if(n instanceof TagNode){
					cleanScript((TagNode)n);
					cleanStyle((TagNode)n);
					t = ((TagNode)n).getText().toString();
//					System.out.println("each:"+t);
					if(t != null){
						if(alltext == null) alltext = t;
						else alltext += t;
					}
				}
			}
		}
		return alltext;
	}
	
	public static String getAttributeValue(TagNode node , String tagXpath , String attr) throws Exception{
		Object[] ns = node.evaluateXPath(tagXpath);
		if(ns != null && ns.length>0)
			return ((TagNode) ns[0]).getAttributeByName(attr);
		return null;
	}
	
	/**
	 * 
	 * <p>
	 * 方法简要描述: 获取标题
	 * </p>
	 * <p>
	 * 方法详细描述: // TODO
	 * </p>
	 * 
	 * @author LinYingPiaoJian(Q:71426052)
	 * @version
	 * @see
	 * @param node
	 * @return
	 */
	public static String getTitle(TagNode node) {
		Object[] ns = node.getElementsByName("title", true);
		if (ns != null && ns.length > 0){
			String title = ((TagNode) ns[0]).getText().toString();
			title = title.replaceAll(" ", "");
			title = title.replaceAll("\n", "");
			title = title.replaceAll("\r", "");
			return title;
		}			
		return null;
	}
	/**
	 * 
	 * <p>
	 * 方法简要描述: 获取第一个href（通常使用在获取下一页）
	 * </p>
	 * <p>
	 * 方法详细描述: // TODO
	 * </p>
	 * 
	 * @author LinYingPiaoJian(Q:71426052)
	 * @version 
	 * @see
	 * @param node
	 * @param xpathfilter
	 * @return
	 * @throws Exception
	 */
	public static String getNextpageUrl(TagNode node , String xpathfilter) throws Exception{
		Object[] ns=node.evaluateXPath(xpathfilter);
		if(ns != null && ns.length >0){
			TagNode node2=(TagNode) ns[0];
			return node2.getAttributeByName("href");
		}
		return null;	
	}
	
	
	
	public static String getAllContent(String href , String encode)throws Exception{	
		HtmlCleaner cleaner = new HtmlCleaner();		
		TagNode node = cleaner.clean(new URL(href), encode);		
		String allcontent = getAllContent(cleaner, node);		
		return allcontent;
	}
	
	public static void main(String[] args) {
		String c = null;
		try {
			String href = "http://mp.weixin.qq.com/s?__biz=MjM5MjQ3NzY5MQ==&mid=205900637&idx=2&sn=dad51c7d3e95782e678d488cfe8e2036&3rd=MzA3MDU4NTYzMw==&scene=6#rd";
			HtmlCleaner cleaner = new HtmlCleaner();
			URL furl = new URL(href);
			TagNode node = cleaner.clean(furl.openStream(), "utf-8");
//			System.out.println(getTagContent(node, "//div[@id='js_content']"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		System.out.println(c);
	}
}
